package com.thundersoft.musicbox;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.*;
import android.widget.*;
import com.thundersoft.musicbox.model.Music;

import java.util.List;

/**
 * 备注:
 *
 * @author hmb
 * @version 1.0
 * @since 2015/11/19 12:18
 */
public class PlayListFragment extends Fragment implements AdapterView.OnItemClickListener{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_playlist, container, false);

        ListView listView = (ListView) view.findViewById(R.id.music_list);
        listView.setOnItemClickListener(this);
        listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
            @Override
            public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

            }

            @Override
            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            @Override
            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }

            @Override
            public void onDestroyActionMode(ActionMode mode) {

            }
        });

        MusicListAdapter adapter = new MusicListAdapter();
        listView.setAdapter(adapter);
        View empty = inflater.inflate(R.layout.music_list_empty, null);
        listView.setEmptyView(empty);
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Music music = MusicContext.getMusicList().get(position);
        FragmentManager manager = getFragmentManager();
        List<Fragment> fragments = manager.getFragments();
        for(Fragment fragment : fragments){
            Log.d("PlayListFragment",fragment.getTag());
        }
    }

    private class MusicListAdapter extends BaseAdapter {
        List<Music> data = MusicContext.getMusicList();

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Music music = data.get(position);
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.music_list_item, parent, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.setData(music);
            return convertView;
        }

    }

    private static class ViewHolder {
        TextView title;
        TextView artist;
        TextView album;

        public ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.title);
            artist = (TextView) view.findViewById(R.id.artist);
            album = (TextView) view.findViewById(R.id.album);
        }

        public void setData(Music music) {
            title.setText(music.getTitle());
            artist.setText(music.getArtist());
            album.setText(music.getAlbum());
        }
    }
}
